load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//bzl:cpp_library.bzl", "cpp_library")
load("//bzl:cpp_unittest.bzl", "cpp_unittest")

package(default_visibility = ["//visibility:public"])

cpp_library(
    name = "test_ssl_server",
    srcs = [
        "TestSSLServer.cpp",
    ],
    hdrs = [
        "TestSSLServer.h",
    ],
    data = [
        "//folly/io/async/test/certs:ca-cert.pem",
        "//folly/io/async/test/certs:client_ca_cert.pem",
        "//folly/io/async/test/certs:client_cert.pem",
        "//folly/io/async/test/certs:client_chain.pem",
        "//folly/io/async/test/certs:client_key.pem",
        "//folly/io/async/test/certs:tests-cert.pem",
        "//folly/io/async/test/certs:tests-key.pem",
    ],
    deps = [
        ":callback_state_enum",
        "//folly:network_address",
        "//folly/io/async:async_base",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_ssl_socket",
        "//folly/io/async:async_transport",
        "//folly/io/async:server_socket",
        "//folly/io/async/ssl:ssl_errors",
        "//folly/portability:gtest",
        "//folly/portability:openssl",
        "//folly/portability:sockets",
        "//folly/portability:unistd",
        "//folly/testing:test_util",
    ],
)

cpp_library(
    name = "tfo_util",
    srcs = [
        "TFOUtil.cpp",
    ],
    hdrs = [
        "TFOUtil.h",
    ],
    deps = [
        "//folly/detail:socket_fast_open",
    ],
)

cpp_library(
    name = "async_signal_handler_test_lib",
    hdrs = [
        "AsyncSignalHandlerTestLib.h",
    ],
    deps = [
        ":util",
        "//folly/io/async:async_base",
        "//folly/io/async:async_signal_handler",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "async_pipe_test",
    srcs = [
        "AsyncPipeTest.cpp",
    ],
    deps = [
        "//folly:memory",
        "//folly/io/async:async_base",
        "//folly/io/async:async_pipe",
        "//folly/portability:gtest",
    ],
)

cpp_library(
    name = "async_ssl_socket_test_lib",
    hdrs = [
        "AsyncSSLSocketTest.h",
    ],
    deps = [
        ":test_ssl_server",
        "//folly:exception_wrapper",
        "//folly:network_address",
        "//folly/fibers:fiber_manager_map",
        "//folly/io:socket_option_map",
        "//folly/io/async:async_base",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_ssl_socket",
        "//folly/io/async:async_transport",
        "//folly/io/async:server_socket",
        "//folly/io/async/ssl:ssl_errors",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
        "//folly/portability:pthread",
        "//folly/portability:sockets",
        "//folly/portability:string",
        "//folly/portability:unistd",
        "//folly/testing:test_util",
    ],
)

# NOTE: Test does not work with OSS distribution.
# cpp_unittest(
#     name = "async_ssl_socket_test",
#     srcs = [
#         "AsyncSSLSocketTest.cpp",
#         "AsyncSSLSocketTest2.cpp",
#     ],
#     # labels = ["oss-broken"],
#     data = [
#         "//folly/io/async/test/certs:ca-cert.pem",
#         "//folly/io/async/test/certs:client_ca_cert.pem",
#         "//folly/io/async/test/certs:client_cert.pem",
#         "//folly/io/async/test/certs:client_key.pem",
#         "//folly/io/async/test/certs:tests-cert.pem",
#         "//folly/io/async/test/certs:tests-key.pem",
#     ],
#     linkopts = select({
#         "@platforms//os:linux": ["-ldl"],
#         "//conditions:default": [],
#     }),
#     deps = [
#         ":async_ssl_socket_test_lib",  # @manual
#         ":blocking_socket",
#         ":mocks",
#         ":test_ssl_server",
#         ":tfo_util",
#         "//folly:exception_wrapper",
#         "//folly:network_address",
#         "//folly:string",
#         "//folly/fibers:fiber_manager_map",
#         "//folly/futures:core",
#         "//folly/init",
#         "//folly/io:iobuf",
#         "//folly/io:socket_option_map",
#         "//folly/io/async:async_base",
#         "//folly/io/async:async_pipe",
#         "//folly/io/async:async_socket",
#         "//folly/io/async:async_ssl_socket",
#         "//folly/io/async:async_transport",
#         "//folly/io/async:event_base_thread",
#         "//folly/io/async:scoped_event_base_thread",
#         "//folly/io/async:server_socket",
#         "//folly/io/async:ssl_context",
#         "//folly/io/async:ssl_options",
#         "//folly/io/async/ssl:basic_transport_certificate",
#         "//folly/io/async/ssl:openssl_transport_certificate",
#         "//folly/io/async/ssl:ssl_errors",
#         "//folly/net:net_ops",
#         "//folly/net:network_socket",
#         "//folly/net/test:mock_net_ops_dispatcher",
#         "//folly/portability:gmock",
#         "//folly/portability:gtest",
#         "//folly/portability:openssl",
#         "//folly/portability:pthread",
#         "//folly/portability:sockets",
#         "//folly/portability:string",
#         "//folly/portability:unistd",
#         "//folly/testing:test_util",
#         "@openssl//:ssl",
#     ],
# )

cpp_unittest(
    name = "async_ssl_socket_write_test",
    srcs = [
        "AsyncSSLSocketWriteTest.cpp",
    ],
    deps = [
        "//folly/io:iobuf",
        "//folly/io/async:async_base",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_ssl_socket",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "async_socket_exception_test",
    srcs = [
        "AsyncSocketExceptionTest.cpp",
    ],
    deps = [
        "//folly:conv",
        "//folly/io/async:async_socket_exception",
        "//folly/io/async:ssl_context",
        "//folly/io/async/ssl:ssl_errors",
        "//folly/portability:gtest",
        "//folly/portability:openssl",
    ],
)

# TODO(kgk): Fix test.
# cpp_unittest(
#     name = "async_socket_observer_test",
#     srcs = [
#         "AsyncSocketObserverTest.cpp",
#     ],
#     deps = [
#         ":async_socket_test_lib",
#         ":mocks",
#         "//folly/portability:gmock",
#         "//folly/portability:gtest",
#     ],
# )

cpp_library(
    name = "async_socket_test_lib",
    hdrs = [
        "AsyncSocketTest.h",
        "AsyncSocketTest2.h",
    ],
    deps = [
        ":blocking_socket",
        ":callback_state_enum",
        ":conn_callback",
        "//folly/io/async:async_socket",
        "//folly/io/async:server_socket",
        "//folly/net:net_ops",
        "//folly/net:network_socket",
        "//folly/portability:sockets",
        "//folly/synchronization:rw_spin_lock",
    ],
)

# Disabled because it takes longer than 15 minutes.
# cpp_unittest(
#     name = "async_socket_test",
#     size = "enormous",
#     srcs = [
#         "AsyncSocketTest.cpp",
#         "AsyncSocketTest2.cpp",
#     ],
#     linkopts = select({
#         "@platforms//os:windows": [],
#         "//conditions:default": ["-lrt"],
#     }),
#     deps = [
#         ":async_socket_test_lib",  # @manual
#         ":blocking_socket",
#         ":callback_state_enum",
#         ":conn_callback",
#         ":mocks",
#         ":tfo_util",
#         ":util",
#         "//folly:exception_wrapper",
#         "//folly:network_address",
#         "//folly:random",
#         "//folly/io:iobuf",
#         "//folly/io:socket_option_map",
#         "//folly/io/async:async_base",
#         "//folly/io/async:async_socket",
#         "//folly/io/async:scoped_event_base_thread",
#         "//folly/io/async:server_socket",
#         "//folly/net:net_ops",
#         "//folly/net:network_socket",
#         "//folly/net/test:mock_net_ops_dispatcher",
#         "//folly/net/test:mock_tcpinfo_dispatcher",
#         "//folly/portability:gmock",
#         "//folly/portability:gtest",
#         "//folly/portability:sockets",
#         "//folly/portability:unistd",
#         "//folly/synchronization:baton",
#         "//folly/synchronization:rw_spin_lock",
#         "//folly/test:socket_address_test_helper",
#         "//folly/testing:test_util",
#     ],
# )

cpp_unittest(
    name = "async_transport_test",
    srcs = [
        "AsyncTransportTest.cpp",
    ],
    deps = [
        ":mocks",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_transport",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "event_handler_test",
    srcs = [
        "EventHandlerTest.cpp",
    ],
    deps = [
        "//folly:mpmc_queue",
        "//folly:scope_guard",
        "//folly/io/async:async_base",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
        "//folly/portability:sockets",
    ],
)

cpp_unittest(
    name = "async_timeout_test",
    srcs = ["AsyncTimeoutTest.cpp"],
    deps = [
        "//folly/io/async:async_base",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "async_udp_socket_test",
    srcs = ["AsyncUDPSocketTest.cpp"],
    deps = [
        "//folly:conv",
        "//folly:network_address",
        "//folly:string",
        "//folly/io:iobuf",
        "//folly/io:socket_option_map",
        "//folly/io/async:async_base",
        "//folly/io/async:async_udp_server_socket",
        "//folly/io/async:async_udp_socket",
        "//folly/net/test:mock_net_ops_dispatcher",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
        "//folly/portability:sockets",
        "//folly/testing:test_util",
    ],
)

cpp_unittest(
    name = "async_udp_socket_gso_gro_test",
    srcs = ["AsyncUDPSocketGSOGROTest.cpp"],
    deps = [
        "//folly:conv",
        "//folly:network_address",
        "//folly/io:iobuf",
        "//folly/io/async:async_base",
        "//folly/io/async:async_udp_server_socket",
        "//folly/io/async:async_udp_socket",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "async_udp_socket_sendmmsg_test",
    srcs = ["AsyncUDPSocketSendmmsgTest.cpp"],
    deps = [
        "//folly:conv",
        "//folly:network_address",
        "//folly/io:iobuf",
        "//folly/io/async:async_base",
        "//folly/io/async:async_udp_server_socket",
        "//folly/io/async:async_udp_socket",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "decorated_async_transport_wrapper_test",
    srcs = ["DecoratedAsyncTransportWrapperTest.cpp"],
    deps = [
        "//folly/io/async:async_socket",
        "//folly/io/async:async_transport",
        "//folly/io/async:decorated_async_transport_wrapper",
        "//folly/portability:gtest",
    ],
)

cpp_library(
    name = "blocking_socket",
    hdrs = ["BlockingSocket.h"],
    deps = [
        "//folly:optional",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_ssl_socket",
        "//folly/io/async:ssl_context",
        "//folly/net:network_socket",
    ],
)

cpp_library(
    name = "callback_state_enum",
    hdrs = [
        "CallbackStateEnum.h",
    ],
)

cpp_library(
    name = "conn_callback",
    hdrs = [
        "ConnCallback.h",
    ],
    deps = [
        ":callback_state_enum",
        "//folly/io/async:async_socket",
    ],
)

cpp_unittest(
    name = "delayed_destruction_base_test",
    srcs = ["DelayedDestructionBaseTest.cpp"],
    deps = [
        "//folly/io/async:delayed_destruction",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "delayed_destruction_test",
    srcs = ["DelayedDestructionTest.cpp"],
    deps = [
        "//folly/io/async:delayed_destruction",
        "//folly/portability:gtest",
        "@glog",
    ],
)

cpp_unittest(
    name = "destructor_check_test",
    srcs = ["DestructorCheckTest.cpp"],
    deps = [
        "//folly:memory",
        "//folly/io/async:destructor_check",
        "//folly/portability:gtest",
    ],
)

cc_binary(
    name = "event_base_benchmark",
    srcs = ["EventBaseBenchmark.cpp"],
    deps = [
        "//folly:benchmark",
        "//folly/io/async:async_base",
        "//folly/portability:gflags",
    ],
)

cpp_library(
    name = "event_base_test_lib",
    hdrs = [
        "EventBaseTestLib.h",
    ],
    deps = [
        ":util",
        "//folly:math",
        "//folly:memory",
        "//folly:scope_guard",
        "//folly/futures:core",
        "//folly/io/async:async_base",
        "//folly/portability:gmock",
        "//folly/portability:stdlib",
        "//folly/portability:unistd",
        "//folly/synchronization:baton",
        "//folly/system:thread_id",
        "//folly/system:thread_name",
    ],
)

# Disabled because this test is real-time timing dependent, and is therefore flaky.
# cpp_unittest(
#     name = "event_base_test",
#     srcs = ["EventBaseTest.cpp"],
#     deps = [
#         ":async_signal_handler_test_lib",
#         ":event_base_test_lib",
#         "//folly/init",
#         "//folly/portability:gtest",
#     ],
# )

cpp_unittest(
    name = "event_base_local_test",
    srcs = ["EventBaseLocalTest.cpp"],
    deps = [
        "//folly/io/async:async_base",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
    ],
)

cpp_unittest(
    name = "event_base_thread_test",
    srcs = ["EventBaseThreadTest.cpp"],
    deps = [
        "//folly/io/async:event_base_manager",
        "//folly/io/async:event_base_thread",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
        "//folly/system:thread_name",
    ],
)

# Disabled because this test is real-time timing dependent, and is therefore flaky.
# cpp_unittest(
#     name = "hhwheel_timer_slow_test",
#     size = "medium",
#     srcs = ["HHWheelTimerSlowTests.cpp"],
#     deps = [
#         ":util",
#         "//folly:random",
#         "//folly/io/async:async_base",
#         "//folly/portability:gtest",
#     ],
# )

# Disabled because this test is real-time timing dependent, and is therefore flaky.
# cpp_unittest(
#     name = "hhwheel_timer_test",
#     srcs = ["HHWheelTimerTest.cpp"],
#     deps = [
#         ":util",
#         "//folly/io/async:async_base",
#         "//folly/portability:gtest",
#     ],
# )

cpp_library(
    name = "mocks",
    hdrs = [
        "MockAsyncSSLSocket.h",
        "MockAsyncSocket.h",
        "MockAsyncSocketLegacyObserver.h",
        "MockAsyncSocketObserver.h",
        "MockAsyncTransport.h",
        "MockAsyncUDPSocket.h",
        "MockTimeoutManager.h",
    ],
    deps = [
        "//folly:memory",
        "//folly/io/async:async_base",
        "//folly/io/async:async_socket",
        "//folly/io/async:async_socket_exception",
        "//folly/io/async:async_ssl_socket",
        "//folly/io/async:async_transport",
        "//folly/io/async:async_udp_socket",
        "//folly/portability:gmock",
    ],
)

cpp_library(
    name = "mock_server_socket",
    hdrs = ["MockAsyncServerSocket.h"],
    deps = [
        "//folly/io/async:async_base",
        "//folly/io/async:server_socket",
        "//folly/portability:gmock",
    ],
)

cpp_unittest(
    name = "notification_queue_test",
    srcs = ["NotificationQueueTest.cpp"],
    deps = [
        "//folly/io/async:async_base",
        "//folly/io/async:scoped_event_base_thread",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
    ],
)

cpp_unittest(
    name = "atomic_notification_queue_test",
    srcs = ["AtomicNotificationQueueTest.cpp"],
    deps = [
        "//folly/io/async:async_base",
        "//folly/portability:gtest",
    ],
)

cc_binary(
    name = "notification_queue_benchmark",
    srcs = ["NotificationQueueBenchmark.cpp"],
    deps = [
        "//folly:benchmark",
        "//folly/io/async:async_base",
        "//folly/synchronization:baton",
    ],
)

cpp_unittest(
    name = "request_context_test",
    srcs = [
        "RequestContextHelper.h",
        "RequestContextTest.cpp",
    ],
    deps = [
        "//folly:memory",
        "//folly:singleton",
        "//folly/container:enumerate",
        "//folly/io/async:async_base",
        "//folly/io/async:request_context",
        "//folly/portability:gtest",
        "//folly/synchronization:relaxed_atomic",
        "//folly/system:thread_name",
        "@boost.thread",
        "@fmt",
    ],
)

cpp_library(
    name = "scoped_bound_port",
    srcs = ["ScopedBoundPort.cpp"],
    hdrs = ["ScopedBoundPort.h"],
    deps = [
        "//folly:memory",
        "//folly:network_address",
        "//folly/io/async:scoped_event_base_thread",
        "//folly/io/async:server_socket",
    ],
)

cpp_unittest(
    name = "scoped_event_base_thread_test",
    srcs = ["ScopedEventBaseThreadTest.cpp"],
    deps = [
        "//folly:optional",
        "//folly/futures:core",
        "//folly/io/async:event_base_manager",
        "//folly/io/async:scoped_event_base_thread",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
        "//folly/system:thread_name",
    ],
)

cpp_unittest(
    name = "terminate_cancellation_token_test",
    srcs = ["TerminateCancellationTokenTest.cpp"],
    deps = [
        "//folly/io/async:terminate_cancellation_token",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
    ],
)

cc_binary(
    name = "socket_client",
    srcs = ["SocketClient.cpp"],
    deps = [
        ":blocking_socket",
        "//folly:exception_wrapper",
        "//folly/portability:gflags",
    ],
)

cpp_unittest(
    name = "ssl_session_test",
    srcs = ["SSLSessionTest.cpp"],
    data = [
        "//folly/io/async/test/certs:ca-cert.pem",
        "//folly/io/async/test/certs:tests-cert.pem",
        "//folly/io/async/test/certs:tests-key.pem",
    ],
    deps = [
        ":async_ssl_socket_test_lib",
        "//folly/net:net_ops",
        "//folly/net:network_socket",
        "//folly/portability:gtest",
        "//folly/portability:openssl",
        "//folly/portability:sockets",
        "//folly/ssl:ssl_session",
        "//folly/ssl/detail:openssl_session",
        "//folly/testing:test_util",
    ],
)

cpp_unittest(
    name = "time_util_test",
    srcs = ["TimeUtilTest.cpp"],
    deps = [
        ":util",
        "//folly/portability:gtest",
        "//folly/portability:unistd",
        "@glog",
    ],
)

cpp_unittest(
    name = "ssl_context_test",
    srcs = ["SSLContextTest.cpp"],
    data = [
        "//folly/io/async/test/certs:client_chain.pem",
        "//folly/io/async/test/certs:client_key.pem",
        "//folly/io/async/test/certs:clienti_key.pem",
        "//folly/io/async/test/certs:tests-cert.pem",
        "//folly/io/async/test/certs:tests-key.pem",
    ],
    deps = [
        ":ssl_util",
        "//folly:file_util",
        "//folly/io/async:ssl_context",
        "//folly/portability:gtest",
        "//folly/portability:openssl",
        "//folly/ssl:openssl_cert_utils",
        "//folly/ssl:openssl_key_utils",
        "//folly/ssl:openssl_ptr_types",
        "//folly/testing:test_util",
    ],
)

cpp_unittest(
    name = "ssl_context_regression_test",
    srcs = ["SSLContextRegressionTest.cpp"],
    deps = [
        "//folly:file_util",
        "//folly/io/async:ssl_context",
        "//folly/portability:gtest",
        "//folly/testing:test_util",
    ],
)

cpp_unittest(
    name = "ssl_options_test",
    srcs = ["SSLOptionsTest.cpp"],
    deps = [
        ":ssl_util",
        "//folly/io/async:ssl_context",
        "//folly/io/async:ssl_options",
        "//folly/portability:gtest",
        "//folly/ssl:openssl_ptr_types",
    ],
)

cpp_library(
    name = "ssl_util",
    srcs = [
        "SSLUtil.cpp",
    ],
    hdrs = [
        "SSLUtil.h",
    ],
    deps = [
        "//folly/ssl:openssl_ptr_types",
    ],
)

# Disabled because this test is real-time timing dependent, and is therefore flaky.
# cpp_unittest(
#     name = "timerfd_timeout_manager_test",
#     srcs = ["TimerFDTimeoutManagerTest.cpp"],
#     deps = [
#         "//folly/io/async:timerfd",
#         "//folly/io/async/test:util",
#         "//folly/portability:gtest",
#     ],
# )

cpp_library(
    name = "util",
    srcs = [
        "SocketPair.cpp",
        "TimeUtil.cpp",
    ],
    hdrs = [
        "SocketPair.h",
        "TimeUtil.h",
        "UndelayedDestruction.h",
        "Util.h",
    ],
    deps = [
        "//folly:conv",
        "//folly:portability",
        "//folly:scope_guard",
        "//folly:string",
        "//folly/net:net_ops",
        "//folly/net:network_socket",
        "//folly/portability:fcntl",
        "//folly/portability:gtest",
        "//folly/portability:sockets",
        "//folly/portability:sys_types",
        "//folly/portability:unistd",
        "//folly/system:thread_id",
        "//folly/test:test_utils",
        "@glog",
    ],
)

cpp_unittest(
    name = "write_chain_async_transport_wrapper_test",
    srcs = ["WriteChainAsyncTransportWrapperTest.cpp"],
    deps = [
        "//folly/io/async:async_transport",
        "//folly/io/async:decorated_async_transport_wrapper",
        "//folly/portability:gmock",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "write_flags_test",
    srcs = ["WriteFlagsTest.cpp"],
    deps = [
        "//folly/io/async:async_transport",
        "//folly/portability:gtest",
    ],
)

cpp_library(
    name = "zero_copy_lib",
    srcs = ["ZeroCopy.cpp"],
    hdrs = ["ZeroCopy.h"],
    deps = [
        "//folly:exception_wrapper",
        "//folly:network_address",
        "//folly/io:iobuf",
        "//folly/io/async:async_base",
        "//folly/io/async:async_socket",
        "//folly/io/async:server_socket",
    ],
)

# NOTE: This fails only in BCR CI.
# TODO(kgk): Fix test.
# cpp_unittest(
#     name = "zero_copy_test",
#     srcs = ["ZeroCopyTest.cpp"],
#     deps = [
#         ":zero_copy_lib",
#         "//folly/portability:gtest",
#     ],
# )

cc_binary(
    name = "zero_copy_benchmark",
    srcs = ["ZeroCopyBenchmark.cpp"],
    deps = [
        ":zero_copy_lib",
        "//folly:benchmark",
        "//folly/portability:gflags",
    ],
)

# Disabled because it relies on real-time timing, so it is inherently flaky depending on CPU load.
# cpp_unittest(
#     name = "hhwheel_timer_high_res_test",
#     srcs = ["HHWheelTimerHighResTest.cpp"],
#     deps = [
#         "//folly/io/async:async_base",
#         "//folly/io/async:timerfd",
#         "//folly/io/async/test:util",
#         "//folly/portability:gtest",
#     ],
# )

cc_binary(
    name = "hhwheel_timer_high_res_benchmark",
    srcs = ["HHWheelTimerHighResBenchmark.cpp"],
    deps = [
        "//folly:benchmark",
        "//folly/io/async:timerfd",
        "//folly/io/async/test:util",
    ],
)

# TODO(kgk): Enable once @libaio is added to bcr. See:
# https://github.com/bazelbuild/bazel-central-registry/issues/3756
# cpp_unittest(
#     name = "async_io_test",
#     srcs = ["AsyncIOTest.cpp"],
#     deps = [
#         "//folly/experimental/io:async_io",
#         "//folly/experimental/io/test:async_base_test_lib",
#     ],
# )

cpp_unittest(
    name = "async_io_uring_socket_test",
    srcs = ["AsyncIoUringSocketTest.cpp"],
    deps = [
        "//folly:file_util",
        "//folly:subprocess",
        "//folly/executors:global_executor",
        "//folly/futures:core",
        "//folly/io/async:async_base",
        "//folly/io/async:async_io_uring_socket",
        "//folly/io/async:async_socket",
        "//folly/io/async:io_uring_backend",
        "//folly/io/async:io_uring_event",
        "//folly/io/async:server_socket",
        "//folly/portability:gtest",
        "//folly/system:shell",
        "//folly/test:socket_address_test_helper",
    ],
)

# Disabled because it relies on real-time timing, so it is inherently flaky depending on CPU load.
# cpp_unittest(
#     name = "epoll_backend_test",
#     srcs = ["EpollBackendTest.cpp"],
#     deps = [
#         "//folly/experimental/io:epoll_backend",
#         "//folly/io/async/test:async_signal_handler_test_lib",
#         "//folly/io/async/test:event_base_test_lib",
#     ],
# )

# TODO(kgk): Enable once @libaio is added to bcr. See:
# https://github.com/bazelbuild/bazel-central-registry/issues/3756
# cc_binary(
#     name = "io_benchmark",
#     srcs = ["IOBenchmark.cpp"],
#     deps = [
#         "//folly:benchmark",
#         "//folly:file_util",
#         "//folly/experimental/io:async_io",
#         "//folly/experimental/io/test:async_base_test_lib",
#         "//folly/experimental/io/test:io_test_temp_file_util_lib",
#         "//folly/io/async:io_uring",
#         "//folly/portability:gflags",
#     ],
# )

# TODO(kgk): Fix test.
# cc_binary(
#     name = "io_uring_backend_bench",
#     srcs = ["IoUringBackendBench.cpp"],
#     deps = [
#         "//folly:benchmark",
#         "//folly:file_util",
#         "//folly/experimental/io:epoll_backend",
#         "//folly/init",
#         "//folly/io/async:async_base",
#         "//folly/io/async:io_uring_backend",
#         "//folly/io/async:scoped_event_base_thread",
#         "//folly/portability:gflags",
#     ],
# )

cpp_unittest(
    name = "io_uring_backend_setup_test",
    srcs = ["IoUringBackendSetupTest.cpp"],
    deps = [
        "//folly/io/async:io_uring_backend",
        "//folly/portability:gtest",
    ],
)

# Disabled because this test is real-time timing dependent, and is therefore flaky.
# cpp_unittest(
#     name = "io_uring_backend_test",
#     srcs = ["IoUringBackendTest.cpp"],
#     deps = [
#         "//folly:file_util",
#         "//folly:function",
#         "//folly:string",
#         "//folly/experimental/io/test:io_test_temp_file_util_lib",
#         "//folly/init",
#         "//folly/io/async:async_base",
#         "//folly/io/async:async_udp_server_socket",
#         "//folly/io/async:async_udp_socket",
#         "//folly/io/async:io_uring_backend",
#         "//folly/io/async/test:async_signal_handler_test_lib",
#         "//folly/io/async/test:event_base_test_lib",
#         "//folly/portability:gtest",
#     ],
# )

cpp_unittest(
    name = "io_uring_event_base_local_test",
    srcs = ["IoUringEventBaseLocalTest.cpp"],
    deps = [
        "//folly/futures:core",
        "//folly/io/async:io_uring_backend",
        "//folly/io/async:io_uring_event_base_local",
        "//folly/portability:gtest",
    ],
)

cpp_unittest(
    name = "io_uring_event_test",
    srcs = ["IoUringEventTest.cpp"],
    deps = [
        "//folly/futures:core",
        "//folly/io/async:async_base",
        "//folly/io/async:io_uring_backend",
        "//folly/io/async:io_uring_event",
        "//folly/portability:gtest",
    ],
)

# NOTE: Test does not work with OSS distribution.
# cpp_unittest(
#     name = "io_uring_test",
#     srcs = ["IoUringTest.cpp"],
#     # labels = ["oss-broken"],
#     deps = [
#         "//folly/experimental/io/test:async_base_test_lib",
#         "//folly/init",
#         "//folly/io/async:io_uring",
#     ],
# )

# TODO(kgk): Fix test.
# cc_binary(
#     name = "registered_fd_benchmark",
#     srcs = ["RegisteredFdBenchmark.cpp"],
#     deps = [
#         "//folly:benchmark",
#         "//folly:file_util",
#         "//folly/io/async:async_base",
#         "//folly/io/async:io_uring_backend",
#         "//folly/portability:gflags",
#     ],
# )

cpp_unittest(
    name = "simple_async_io_test",
    srcs = ["SimpleAsyncIOTest.cpp"],
    deps = [
        "//folly:file",
        "//folly:random",
        "//folly/coro:blocking_wait",
        "//folly/coro:collect",
        "//folly/experimental/io:simple_async_io",
        "//folly/io:iobuf",
        "//folly/portability:gtest",
        "//folly/synchronization:baton",
        "@glog",
    ],
)

cpp_library(
    name = "io_test_temp_file_util_lib",
    srcs = ["IoTestTempFileUtil.cpp"],
    hdrs = ["IoTestTempFileUtil.h"],
    deps = [
        "//folly:file_util",
        "//folly:string",
        "//folly/experimental/io:fs_util",
        "//folly/testing:test_util",
        "@glog",
    ],
)

cpp_library(
    name = "async_base_test_lib",
    srcs = ["AsyncBaseTestLib.cpp"],
    hdrs = ["AsyncBaseTestLib.h"],
    deps = [
        "//folly:scope_guard",
        "//folly:string",
        "//folly/experimental/io:async_base",
        "//folly/experimental/io:fs_util",
        "//folly/experimental/io/test:io_test_temp_file_util_lib",
        "//folly/portability:gtest",
        "//folly/portability:sockets",
        "//folly/portability:unistd",
        "//folly/test:test_utils",
        "@glog",
    ],
)

cpp_library(
    name = "mux_io_thread_pool_executor_test_lib",
    srcs = ["MuxIOThreadPoolExecutorTest.cpp"],
    deps = [
        "//folly/executors/test:IOThreadPoolExecutorBaseTestLib",
        "//folly/experimental/io:epoll",
        "//folly/experimental/io:mux_io_thread_pool_executor",
        "//folly/portability:gtest",
        "//folly/synchronization:latch",
    ],
)

cpp_unittest(
    name = "mux_io_thread_pool_executor_test_epoll",
    args = [
        "--folly_event_base_poller_backend=epoll",
        "--folly_event_base_poller_epoll_rearm_inline=false",
    ],
    deps = [
        ":mux_io_thread_pool_executor_test_lib",  # @manual
    ],
)

cpp_unittest(
    name = "mux_io_thread_pool_executor_test_epoll_rearm_inline",
    args = [
        "--folly_event_base_poller_backend=epoll",
        "--folly_event_base_poller_epoll_rearm_inline=true",
    ],
    deps = [
        ":mux_io_thread_pool_executor_test_lib",  # @manual
    ],
)

cpp_unittest(
    name = "mux_io_thread_pool_executor_test_io_uring",
    args = [
        "--folly_event_base_poller_backend=io_uring",
    ],
    deps = [
        ":mux_io_thread_pool_executor_test_lib",  # @manual
    ],
)
